﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using 开单.Model.基础;
using System.Data;
using winform;
using System.Data.OleDb;

namespace 开单.Dao.基础
{
    public class KhxxDao
    {
        KhxxEntity khxxEntity = new KhxxEntity();
        DataTable dt = new DataTable();
        /// <summary>
        /// 添加客户
        /// </summary>
        /// <param name="KhxxEntity">客户实体类</param>
        public void AddKhxx(KhxxEntity khxxEntity)
        {
            string sql = string.Empty;
            OleDbParameter[] parameters = {
                     new OleDbParameter("@kh_name", khxxEntity.kh_name),
                     new OleDbParameter("@yg_name", khxxEntity.yg_name),
                     new OleDbParameter("@kh_dh", khxxEntity.kh_dh),
                     new OleDbParameter("@kh_dz", khxxEntity.kh_dz),
                     new OleDbParameter("@kh_jp", khxxEntity.kh_jp)};

            sql = "insert into jc_khxx(kh_name,yg_name,kh_dh,kh_dz,kh_jp) values " +
                    "(@kh_name,@yg_name,@kh_dh,@kh_dz,@kh_jp)";

            DbHelperOleDb.ExecuteSql(sql, parameters);
        }

        /// <summary>
        /// 删除客户
        /// </summary>
        /// <param name="KhxxEntity">客户实体类</param>
        public void DelKhxx( object kh_code)
        {
            string sql = string.Empty;
            OleDbParameter[] parameters = {
                     new OleDbParameter("@kh_code", kh_code)};

            sql = "delete from jc_khxx where kh_code=@kh_code ";

            DbHelperOleDb.ExecuteSql(sql, parameters);
        }

        /// <summary>
        /// 查询客户信息
        /// </summary>
        /// <param></param>
        /// <returns></returns>
        public DataTable GetKhxxDt(string kh_jp)
        {
            kh_jp = "%" + kh_jp + "%";
            OleDbParameter[] paramethers ={
                                             new OleDbParameter("@kh_jp",kh_jp)};
            string sql = "select kh_code as 客户编码,kh_name as 客户名称,yg_name as 业务员,kh_dh as 客户电话," +
                "kh_dz as 客户地址,kh_jp as 简拼" +
              " from jc_khxx where kh_jp like @kh_jp ";
            return DbHelperOleDb.ExecuteWithDataTable(sql, paramethers);
        }

        /// <summary>
        /// 查询客户信息
        /// </summary>
        /// <param></param>
        /// <returns></returns>
        public DataTable GetKhxxDtByKhName(string kh_name)
        {
            OleDbParameter[] paramethers ={
                                             new OleDbParameter("@kh_name",kh_name)};
            string sql = "select kh_code as 客户编码,kh_name as 客户名称,yg_name as 业务员,kh_dh as 客户电话," +
                "kh_dz as 客户地址,kh_jp as 简拼" +
              " from jc_khxx where kh_name =@kh_name ";
            return DbHelperOleDb.ExecuteWithDataTable(sql, paramethers);
        }

        /// <summary>
        /// 更新客户信息
        /// </summary>
        /// <param></param>
        /// <returns></returns>
        public void UpdataKhxxByKh_code(KhxxEntity khxxEntity)
        {
            OleDbParameter[] parameters = {
                     new OleDbParameter("@kh_name", khxxEntity.kh_name),
                     new OleDbParameter("@yg_name", khxxEntity.yg_name),
                     new OleDbParameter("@kh_dh", khxxEntity.kh_dh),
                     new OleDbParameter("@kh_dz", khxxEntity.kh_dz),
                     new OleDbParameter("@kh_jp", khxxEntity.kh_jp),
                     new OleDbParameter("@kh_code", khxxEntity.kh_code)};
            string sql = "update jc_khxx set kh_name=@kh_name,yg_name=@yg_name,kh_dh=@kh_dh,kh_dz=@kh_dz,kh_jp=@kh_jp where kh_code=@kh_code ";
            DbHelperOleDb.ExecuteSql(sql, parameters);
        }

        //public int getInfoCount()
        //{
        //    string sql = "select count(*) from xs_khxx ";
        //    dt = DbHelperOleDb.ExecuteWithDataTable(sql, null);
        //    int i = Convert.ToInt32(dt.Rows[0][0]);
        //    return i;
        //}
    }
}
